/*
Copyright (C) 2009-2010 Xanadu Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "../Messenger.h"
#include "../../tools/PacketCreator.h"
#include "PlayerHandler.h"
#include "../../server/Player.h"
#include "../world/World.h"

void PlayerHandler::handleMessenger(PacketReader& packet) {
	Messengers *messengers = player->getWorld()->getMessengers();
	char mode = packet.read();
	switch (mode) {
			case 0x00: { // open
				if (player->getMessenger() != 0) {
					return;
				}
				int messengerId = packet.readInt();
				if (messengerId == 0) {
					player->setMessengerPosition(0);
					messengers->create(player);
				} else { // join
					messengers->join(player, messengerId);
				}
				break;
			}
			case 0x02: // leave
				if (player->getMessenger() != 0) {
					if (player->getMessenger()->getMembersCount() == 1) {
						player->getWorld()->getMessengers()->remove(player->getMessenger()->getId());
						player->getMessenger()->getMembers()->deleteMember(player->getID());
						delete player->getMessenger();
					} else {
						player->getMessenger()->getMembers()->send(PacketCreator().removeMessengerPlayer(player->getMessengerPosition()), player);
						player->getMessenger()->getMembers()->deleteMember(player->getID());
					}
					player->setMessenger(0);
				}
				break;
			case 0x03: { // invite
				if (player->getMessenger()->getMembersCount() == 3) {
					player->send(PacketCreator().messengerChat(player->getName() + " : You cannot have more than 3 people in the Maple Messenger"));
					return;
				}
				string input = packet.readString();
				Player *target = player->getWorld()->getPlayerByName(input);
				if (target != 0) {
					if (target->getMessenger() == 0) {
						target->send(PacketCreator().messengerInvite(player->getName(), player->getMessenger()->getId()));
						player->send(PacketCreator().messengerNote(input, 4, 1));
					} else {
						player->send(PacketCreator().messengerChat(player->getName() + " : " + input + " is already using Maple Messenger"));
					}
				} else {
					player->send(PacketCreator().messengerNote(input, 4, 0));
				}
				break;
			}
			case 0x05: { // decline
				string targeted = packet.readString();
				Player *target = player->getWorld()->getPlayerByName(targeted);
				if (target != 0) {
					if (target->getMessenger() != 0) {
						target->send(PacketCreator().messengerNote(player->getName(), 5, 0));
					}
				}
				break;
			}
			case 0x06: // chat
				if (player->getMessenger() != 0) {
					string input = packet.readString();
					player->getMessenger()->getMembers()->send(PacketCreator().messengerChat(input), player);
				}
				break;
	}
}